package com.bestcem.xm.ticket.dao.impl.mysql;

import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketWechatDao;
import com.bestcem.xm.ticket.dao.mapper.TicketWechatDaoMapper;
import com.bestcem.xm.ticket.entity.mysql.TicketWechatDO;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ds_ticket_wechat 相关DAO层方法
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version 通知提醒新增微信公众号
 * @date 2022/4/1 18:46
 */
@Slf4j
@Repository
public class TicketWechatDaoImpl implements TicketWechatDao {

    @Resource
    private TicketWechatDaoMapper ticketWechatDaoMapper;

    /**
     * 获取单个微信用户信息
     *
     * @param openId 微信openId
     * @return 微信用户信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 19:34
     */
    @Nullable
    @Override
    public TicketWechatDO findByOpenId(String openId) {
        return ticketWechatDaoMapper.selectOneByOpenId(openId);
    }


    /**
     * 批量获取微信用户信息
     *
     * @param unionIds 微信unionId列表
     * @return 微信用户信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/1 18:50
     */
    @NotNull
    @Override
    public List<TicketWechatDO> listByUnionIds(List<String> unionIds) {
        if (CollectionUtils.isEmpty(unionIds)) {
            return new ArrayList<>();
        }
        // 去掉列表中的null
        unionIds = unionIds.stream().filter(Objects::nonNull).collect(Collectors.toList());
        return ticketWechatDaoMapper.selectListByUnionIds(unionIds);
    }


    /**
     * 批量获取微信用户信息
     *
     * @param openIds 微信openId列表
     * @return 微信用户信息
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 11:18
     */
    @Nonnull
    @Override
    public List<TicketWechatDO> listByOpenIds(List<String> openIds) {
        if (CollectionUtils.isEmpty(openIds)) {
            return new ArrayList<>();
        }
        // 去掉列表中的null
        openIds = openIds.stream().filter(Objects::nonNull).collect(Collectors.toList());
        return ticketWechatDaoMapper.selectListByOpenIds(openIds);
    }


    /**
     * 插入单条微信用户信息
     *
     * @param isDo 微信用户信息实体
     * @return 插入的成功与否
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2022/4/7 13:15
     */
    @Override
    public boolean save(TicketWechatDO isDo) {
        if (Objects.isNull(isDo.getSample())) {
            isDo.setSample(false);
        }
        if (Objects.isNull(isDo.getCreateTime())) {
            isDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(isDo.getUpdateTime())) {
            isDo.setUpdateTime(DateUtil.getCommonDate());
        }

        return ticketWechatDaoMapper.insert(isDo) == 1;
    }
}
